package cloud.xuxiaowei.core.utils;

import cloud.xuxiaowei.core.annotation.ControllerAnnotation;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;

import java.lang.annotation.Annotation;

/**
 * @author xuxiaowei
 * @since 0.0.1
 */
public class MethodParameterUtils {

	public static String getMethodName(MethodParameter methodParameter) {
		Annotation[] methodAnnotations = methodParameter.getMethodAnnotations();
		for (Annotation annotation : methodAnnotations) {
			if (annotation.annotationType().equals(ControllerAnnotation.class)) {
				ControllerAnnotation controllerAnnotation = (ControllerAnnotation) annotation;

				String value = controllerAnnotation.value();
				if (StringUtils.hasText(value)) {
					return value;
				}
			}

			// if (annotation.annotationType().equals(Operation.class)) {
			// Operation operation = (Operation) annotation;
			//
			// String summary = operation.summary();
			// if (StringUtils.hasText(summary)) {
			// return summary;
			// }
			//
			// String description = operation.description();
			// if (StringUtils.hasText(description)) {
			// return description;
			// }
			// }
		}
		return null;
	}

	@SuppressWarnings({ "unchecked" })
	public static <T extends Annotation> T getAnnotation(MethodParameter methodParameter, Class<T> annotationClass) {
		Annotation[] methodAnnotations = methodParameter.getMethodAnnotations();
		for (Annotation annotation : methodAnnotations) {
			if (annotation.annotationType().equals(annotationClass)) {
				return (T) annotation;
			}
		}
		return null;
	}

}
